package ieco.daoimpl;

import ieco.domaine.Etudiant;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@SuppressWarnings("serial")
@Stateless
@LocalBean
public class EtudiantDaoImpl extends DaoImpl<String, Etudiant> {

	@PersistenceContext(unitName="options")
	private EntityManager entityManager;
	
	public EtudiantDaoImpl() {
		super(Etudiant.class);
	}

	public Etudiant findEtudiant(int matricule) {
		String query = "select e from Etudiant e where e.matricule = ?1";
		return find(query, matricule);
	}

	public int deleteEtudiant(int matricule){
		Query query = entityManager.createQuery("Delete from Etudiant e where e.matricule = ?1");
		query.setParameter(1, matricule);
		return query.executeUpdate();
	}
	
	public List<Etudiant> findAllEtudiantsPourSerie(int serieId){
		String query = "select e from Etudiant e where e.serie.id = ?1";
		return findAll(query, serieId);
	}
	
	public List<Etudiant> findAllEtudiantsByAnnee(int annee){
		String query = "select e from Etudiant e where e.annee = ?1";
		return findAll(query, annee);
	}
	
	public List<Etudiant> findAllEtudiants() {
		String query = "select e from Etudiant e";
		return findAll(query);
	}
	
	public List<Etudiant> findAllEtudiantsForExport(){
		String query = "select e from Etudiant e, Serie s where e.serie.id = s.id order by s.id, e.numero, e.nom, e.prenom";
		return findAll(query);
	}
}
